# coding: utf-8
import pymysql


class ConfigureFile:

    def __init__(self):
        self.current_environment = 'line'  # line or test line线上环境 test 测试环境
        self.db_name, self.db_host, self.db_port, self.db_user, self.db_passwd = 'automation_db', '139.227.255.197', '8012', 'huangshunyao', 'xiaoxi5332#'
        self.default_configure = {  # 默认线上
            'default': {
                'ENGINE': 'django.db.backends.mysql',
                'NAME': 'framework_celery',
                'USER': 'huangshunyao',
                'PASSWORD': '123456',
                'HOST': '139.227.255.197',
                'PORT': 8012,
            }
        }
        self.rabbitmq_configure = 'pyamqp://huangshunyao:123456@139.227.255.197//'

    def get_configure_file(self, configure_name, environment_name):
        if configure_name == 'mysql':
            return self.mysql_configure_fun(environment_name) if environment_name else self.mysql_configure_fun(self.current_environment)
        elif configure_name == 'setting_mysql':
            return self.setting_configure_fun(environment_name) if environment_name else self.setting_configure_fun(self.current_environment)
        elif configure_name == 'rabbit':
            return self.rabbitmq_configure_fun(environment_name) if environment_name else self.rabbitmq_configure_fun(self.current_environment)
        else:
            print('configure_name不存在,请传递正确的configure_name')
            return False

    def setting_configure_fun(self, configure_name):
        default_configure = self.default_configure['default']
        if configure_name == 'localhost':
            default_configure['USER'], default_configure['PASSWORD'], default_configure['HOST'], default_configure['PORT'] = 'root', '123456', 'localhost', '3306'
        elif configure_name == 'test':
            default_configure['USER'], default_configure['PASSWORD'], default_configure['HOST'], default_configure['PORT'] = 'huangshunyao', '123456', '192.168.1.5', '8012'
        return self.default_configure

    def mysql_configure_fun(self, configure_name):
        if configure_name == 'localhost':
            self.db_host, self.db_port, self.db_user, self.db_passwd = 'localhost', 3306, 'root', '123456'
        elif configure_name == 'test':
            self.db_host, self.db_port, self.db_user, self.db_passwd = '192.168.1.5', 8012, 'huangshunyao', '123456'
        return pymysql.connect(host=self.db_host, port=int(self.db_port), user=self.db_user, passwd=self.db_passwd, db=self.db_name, charset='utf8', )

    def rabbitmq_configure_fun(self, configure_name):
        if configure_name == 'localhost':
            self.rabbitmq_configure = 'pyamqp://guest@localhosts//'
        elif configure_name == 'test':
            self.rabbitmq_configure = 'pyamqp://huangshunyao:123456@192.168.1.5//'
        return self.rabbitmq_configure


if __name__ == "__main__":
    pass
